package com.soulapp.hook.hooker

import android.widget.Toast
import com.highcapable.kavaref.KavaRef.Companion.resolve
import com.highcapable.kavaref.condition.type.Modifiers
import com.highcapable.yukihookapi.hook.core.YukiMemberHookCreator.MemberHookCreator.Result
import com.highcapable.yukihookapi.hook.log.YLog
import com.soulapp.data.ConfigData

// 解除聊天送礼物
object LimitChatHook : BaseHooker() {
    private const val TAG = "LimitChatHook"
    private var hook: Result? = null

    override fun getKey() = ConfigData.limitChat

    override fun onHook() {
        startHook()
    }

    override fun startHook() {
        YLog.debug("startHook $TAG $hookEnabled")
        if (!hookEnabled) return

        val className = "cn.soulapp.android.lib.common.bean.ChatLimitModel"
        hook = className.toClass()
            .resolve()
            .optional()
            .method {
                modifiers(Modifiers.PUBLIC)
                name("isLimit")
                emptyParameters()
                returnType = Boolean::class.javaPrimitiveType
            }.first()
            .hook {
                replaceAny {
                    val showToast = ConfigData.getBoolean(ConfigData.showToast)
                    if (showToast) {
                        Toast.makeText(appContext, "解除聊天送礼", Toast.LENGTH_SHORT).show()
                    }
                    false
                }
            }
    }

    override fun stopHook() {
        if (hookEnabled) return
        hook?.let {
            it.remove {
                YLog.debug("stopHook $TAG 成功")
            }
        } ?: YLog.debug("stopHook $TAG 失败")

    }

}